package com.xiaowu.netmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowu.netmusic.domain.po.PlaylistSong;
import com.xiaowu.netmusic.domain.po.Song;
import com.xiaowu.netmusic.mapper.PlaylistMapper;
import com.xiaowu.netmusic.mapper.PlaylistSongMapper;
import com.xiaowu.netmusic.mapper.SongMapper;
import com.xiaowu.netmusic.service.PlaylistSongService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/4/29 21:28
 */
@Service
@RequiredArgsConstructor
public class PlaylistSongServiceImpl extends ServiceImpl<PlaylistSongMapper, PlaylistSong> implements PlaylistSongService {

    private final PlaylistSongMapper playlistSongMapper;

    // 添加歌曲到歌单
    public void addSongToPlaylist(Long playlistId, Long songId) {
        PlaylistSong playlistSong = new PlaylistSong();
        playlistSong.setPlaylistId(playlistId);
        playlistSong.setSongId(songId);
        playlistSongMapper.insert(playlistSong); // 插入记录到关联表
    }

    // 从歌单中移除歌曲
    public void removeSongFromPlaylist(Long playlistId, Long songId) {
        int rows = playlistSongMapper.delete(
                new LambdaQueryWrapper<PlaylistSong>()
                        .eq(PlaylistSong::getPlaylistId, playlistId)
                        .eq(PlaylistSong::getSongId, songId)
        );
        if (rows == 0) {
            throw new RuntimeException("歌曲未添加到此歌单");
        }
    }

    // 获取歌单中的所有歌曲
    public List<PlaylistSong> getSongsInPlaylist(Long playlistId) {
        return playlistSongMapper.selectList(
                new LambdaQueryWrapper<PlaylistSong>().eq(PlaylistSong::getPlaylistId, playlistId)
        );
    }

}
